package com.mimc.controller.admin;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Inject;
import com.jfinal.core.paragetter.Para;
import com.mimc.core.model.SysMenu;
import com.mimc.core.service.SysMenuService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.service.ShSysMenuService;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@RequestMapping("/admin/sys/menu")
@Slf4j
public class SysMenuController extends JbootController {
    @Inject
    private SysMenuService sysMenuService;
    @Inject
    private ShSysMenuService shSysMenuService;

    public AjaxResult menuList(){
        List<SysMenu> menuList=sysMenuService.findListByColumns(Columns.create(),"sort asc");
        List<SysMenu> menutree=shSysMenuService.buildMenuTree(menuList);
        return AjaxResult.success(menutree);
    }
    public AjaxResult add(@Para("") SysMenu sysMenu){
        log.info("表单数据{}", JSON.toJSONString(sysMenu));
        sysMenu.setCreateTime(new Date());
        sysMenu.setUpdateTime(new Date());
        sysMenuService.save(sysMenu);
        return AjaxResult.success("添加成功");
    }
    public AjaxResult detail(Integer menuId){
        SysMenu sysMenu=sysMenuService.findById(menuId);
        return AjaxResult.success("添加成功",sysMenu);
    }
    public AjaxResult update(@Para("") SysMenu sysMenu){
        sysMenu.setUpdateTime(new Date());
        sysMenuService.update(sysMenu);
        return AjaxResult.success("添加成功");
    }

    public AjaxResult delete(Integer menuId){
        SysMenu sysMenu=sysMenuService.findById(menuId);
        List<SysMenu> childList=sysMenuService.findListByColumns(Columns.create("parent_id",sysMenu.getMenuId()));
        if(childList!=null&&childList.size()>0){
            return AjaxResult.error("含有子菜单不能删除");
        }
        sysMenuService.delete(sysMenu);
        return AjaxResult.success("删除成功");
    }
}
